const DB = require('../../utils/db');
const { cosDelete, cosUpload } = require('../../utils/oss');
const path = require('path');

// 文件夹名称
let ossDirectory = 'album';

module.exports = {
	list: async (pageNum, pageSize, keywords) => {
		let [allList] = await DB.query(`select *  from t_album where name like '%${keywords}%'`);

		let arr = [(pageNum - 1) * pageSize, pageSize];
		let [list] = await DB.query(
			`select a.*,b.nums, date_format(a.createTime,'%Y-%m-%d %H:%i:%s') as createTime ,date_format(a.updateTime,'%Y-%m-%d %H:%i:%s') as updateTime  
                   from t_album a left join(SELECT albumId,  count(1) nums FROM t_pic GROUP BY albumId) b on a.id=b.albumId where a.name like '%${keywords}%' limit ?,?`,
			arr
		);
		return { data: list, total: allList.length };
	},
	add: async (name, directoryName, url, filename, isPwd, password, remark) => {
		let [list] = await DB.query('select * from t_album where name=?', [name]);

		if (list.length) {
			throw new Error('相册名称已存在');
		}

		// 上传oss
		let { Location } = await cosUpload(ossDirectory, filename);

		// oss 全路径
		let fullUrl = 'https://' + Location;

		await DB.query('insert into t_album ( name,directoryName, url,fullUrl, isPwd, password, remark) values(?,?,?,?,?,?,?)', [name, directoryName, url, fullUrl, isPwd, password, remark]);
	},
	edit: async (id, name, url, filename, isPwd, password, remark) => {
		let [list] = await DB.query('select url from t_album where id=?', [id]);
		// 未修改封面，就不上传oss
		if (list[0].url == url) {
			await DB.query(`update t_album set name=?,isPwd=?,password=?,remark=? where id =?`, [name, isPwd, password, remark, id]);
		} else {
			await cosDelete(ossDirectory, path.basename(list[0].url));

			// 上传oss
			let { Location } = await cosUpload(ossDirectory, filename);
			// oss 全路径
			let fullUrl = 'https://' + Location;

			// 修改新图片
			await DB.query(`update t_album set  url=?,fullUrl=?,name=?,isPwd=?,password=?,remark=? where id =?`, [url, fullUrl, name, isPwd, password, remark, id]);
		}
	},
	detail: async (id) => {
		const [list] = await DB.query(`select * from t_album where id =?`, [id]);
		return list[0];
	},
	del: async (id) => {
		let [pic] = await DB.query('select * from t_pic where albumId=?', [id]);
		if (pic.length) {
			throw new Error('该相册下有图片，请先删除图片');
		}
		let [list] = await DB.query('select url from t_album where id=?', [id]);
		// 删除oss 图片
		await cosDelete(ossDirectory, path.basename(list[0].url));
		await DB.query(`delete from t_album where id =?`, [id]);
		return true;
	},
	pic: async (id) => {
		let [list] = await DB.query('select * from t_pic where albumId=?', [id]);
		return list;
	},
	delPic: async (id) => {
		let [list] = await DB.query('select * from t_pic where id=?', [id]);
		let name = path.basename(list[0].fullUrl);
		let picDirectoryName = list[0].fullUrl.split('album/')[1].split('/')[0];

		// 删除oss 图片
		await cosDelete(ossDirectory + '/' + picDirectoryName, name);
		await DB.query(`delete from t_pic where id =?`, [id]);
		return true;
	}
};
